

from django.shortcuts import render, redirect,reverse

from wepapi.models import FileModal
from wepapi.utils import form
from wepapi.utils.Pagination import Pagination
from wepapi.utils.bootstrap import BootStrapModelForm


def city_list(request):
    fileList = FileModal.objects.all()
    page_object = Pagination(request, fileList)
    context = {
        "queryset": page_object.page_queryset,  # 分完页的数据
        "page_string": page_object.html(),  # 生成页码

    }

    return render(request, 'city_list.html', context)


class CityAddModelForm(BootStrapModelForm):
    bootstrap_exclude_fields = ['img']

    class Meta:
        model = FileModal
        fields = '__all__'

    pass


def city_add(request):
    if request.method == 'GET':
        title = "新建城市"
        form = CityAddModelForm()
        context = {
            'title': title,
            'uform': form,
        }

        return render(request, 'city_add.html', context)

    if request.method == 'POST':
        uform = CityAddModelForm(data=request.POST, files=request.FILES)
        if uform.is_valid():
            # 对于文件：自动保存
            # 字段 + 上传路径写入到数据库
            uform.save()
            return redirect(reverse("city_list"))
        return render(request, 'city_add.html', {uform: uform, 'title': '新建城市', })
